package com.fenghuajueli.idiomppp.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.fenghuajueli.idiomppp.R;
import com.fenghuajueli.idiomppp.entity.GuessAnswerEntity;
import com.fenghuajueli.idiomppp.listener.OnGuessListClickListener;
import com.fenghuajueli.libbasecoreui.listener.OnBaseClick;


import java.util.ArrayList;
import java.util.List;

/**
 * 看图猜成语底部填充文字数据适配器
 */
public class IdiomGuessFillTextGridAdapter extends BaseAdapter {

    private List<GuessAnswerEntity> guessAnswerEntityList;
    private Context mContext;
    private LayoutInflater mInflater;
    private int itemWidth;
    private OnGuessListClickListener<GuessAnswerEntity> onGuessListClickListener;
    private List<GuessAnswerEntity> alreayFillAnswerEntityList;
    private OnBaseClick<String> onBaseClick;
    private int wordNum = 0;


    public IdiomGuessFillTextGridAdapter(Context context, List<GuessAnswerEntity> guessAnswerEntityList,int wordNum) {
        this.guessAnswerEntityList = guessAnswerEntityList;
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.wordNum = wordNum;
        alreayFillAnswerEntityList = new ArrayList<>();
        itemWidth = (ScreenUtils.getScreenWidth() - ConvertUtils.dp2px((8 - 1) * 6) - ConvertUtils.dp2px(66)) / 8;
    }

    public void refreshData(List<GuessAnswerEntity> guessAnswerEntityList) {
        this.guessAnswerEntityList = guessAnswerEntityList;
        notifyDataSetChanged();
    }

    public void setOnListClickListener(OnGuessListClickListener<GuessAnswerEntity> onGuessListClickListener) {
        this.onGuessListClickListener = onGuessListClickListener;
    }

    public void setOnBaseClick(OnBaseClick<String> onBaseClick) {
        this.onBaseClick = onBaseClick;
    }

    @Override
    public int getCount() {
        return guessAnswerEntityList.size();
    }

    @Override
    public Object getItem(int i) {
        return guessAnswerEntityList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.idiom_fill_text_item_layout, null);
            holder.idiomText = convertView.findViewById(R.id.idiomText);
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(itemWidth, itemWidth);
            convertView.setLayoutParams(layoutParams);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        GuessAnswerEntity idiomPinEntity = guessAnswerEntityList.get(position);
        if (idiomPinEntity.getSelect()) {
            holder.idiomText.setVisibility(View.GONE);
        } else {
            holder.idiomText.setVisibility(View.VISIBLE);
        }
        holder.idiomText.setText(idiomPinEntity.getWord());
        Typeface tf = Typeface.createFromAsset(mContext.getAssets(), "fonts/baotuxiaobaiti.ttf");
        holder.idiomText.setTypeface(tf);
        holder.idiomText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                if (!QuickClickUtils.isFastClick(200)) {
//                    return;
//                }
                if (alreayFillAnswerEntityList.size() < wordNum) {
                    idiomPinEntity.setSelect(true);
                    alreayFillAnswerEntityList.add(idiomPinEntity);
                    notifyDataSetChanged();
                    onGuessListClickListener.itemClick(position, idiomPinEntity,wordNum);
                }
                if (alreayFillAnswerEntityList.size()== wordNum) {
                    String data="";
                    for (int i = 0; i <alreayFillAnswerEntityList.size() ; i++) {
                        data = data + alreayFillAnswerEntityList.get(i).getWord();
                    }
                    if (onBaseClick!=null) {
                        onBaseClick.onClick(data);
                    }
                }
            }
        });
        return convertView;
    }

    final class ViewHolder {
        TextView idiomText;
    }

    /**
     * 点击填充过得字时  取消填充到列表，恢复到底部可选的字列表
     *
     * @param text
     */
    public void recoveryItem(String text) {
        for (int i = 0; i < guessAnswerEntityList.size(); i++) {
            GuessAnswerEntity idiomPinEntity = guessAnswerEntityList.get(i);
            if (idiomPinEntity.getWord().equals(text)) {
                alreayFillAnswerEntityList.remove(idiomPinEntity);
                idiomPinEntity.setSelect(false);
            }
        }
        notifyDataSetChanged();
    }

}
